package org.tr.demo.clock;// File   : GUI-lowlevel/animation/analogclock/Clock.java
// Purpose: An analog clock component -- Uses Timer and Calendar.
// Note   : Uses a BufferedImage for clock face so isn't drawn each time.
// Author : Fred Swartz, 1999-2007, Placed in public domain.

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.image.*;
import java.util.Timer;

//////////////////////////////////////////////////////////////// Clock class
class DigitalClock extends JComponent {
    
    private static final int    UPDATE_INTERVAL = 1;  // Millisecs
    private Calendar _now = Calendar.getInstance();  // Current time.
    
    private javax.swing.Timer _timer;      // Fires to update clock.
    private List<Integer> list = new ArrayList<Integer>();
    private Timer timer;
    private boolean outOfMemory = false;

    //==================================================== Clock constructor
    public DigitalClock() {
        setPreferredSize(new Dimension(1000,400));
        
        _timer = new javax.swing.Timer(UPDATE_INTERVAL, new ActionListener() {
            public void actionPerformed(ActionEvent e) {            	
                updateTime();
                repaint();
            }
        });

        timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask(){
            @Override
            public void run() {
                try {
                    for (int i=0; i<1024L*2L; i++) {
                        list.add(i);
                    }
                } catch (OutOfMemoryError e) {
                    outOfMemory=true;
                }

            }
        },0,10000);

    }
    
    //================================================================ start
    /** Start the timer. */
    public void start() {
        _timer.start(); 
    }
    
    //================================================================= stop
    /** Stop the timer. */
    public void stop() {
        _timer.stop(); 
    }
    
    //=========================================================== updateTime
    private void updateTime() {
        //... Avoid creating new objects.
        _now.setTimeInMillis(System.currentTimeMillis());
        //System.out.println(new Date(_now.getTimeInMillis()));
    }
    
    //======================================================= paintComponent
    @Override public void paintComponent(Graphics g) {
        
    	Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
    	
        int fontSize = 100;
        int w = getWidth();
        int h = getHeight();
        

        if (outOfMemory) {
            g.setFont(new Font("TimesRoman", Font.PLAIN, fontSize));
            g.setColor(Color.RED);
            g.drawString("OUT OF MEMORY", w/2-14*fontSize/4, h/2);
            //e.printStackTrace();
        } else {
            //Calendar cal = Calendar.getInstance();
            //Date date = cal.getTime();
            g.setFont(new Font("TimesRoman", Font.PLAIN, fontSize));
            g.setColor(Color.BLUE);
            DateFormat dateFormatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
            String dateStr = dateFormatter.format(new Date());
            g.drawString(dateStr, w/2-dateStr.length()*fontSize/4, h/2);
        }
    }
}
